package com.miukoo.aikao.qmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miukoo.aikao.core.dtos.PageReqDto;
import com.miukoo.aikao.core.dtos.PageResDto;
import com.miukoo.aikao.qmanage.dto.RepositoryQuestionDto;
import com.miukoo.aikao.qmanage.dto.request.RepositoryQuestionBatchReqDto;
import com.miukoo.aikao.qmanage.mapper.QuestionMapper;
import com.miukoo.aikao.qmanage.mapper.RepositoryQuestionMapper;
import com.miukoo.aikao.core.pojos.Question;
import com.miukoo.aikao.qmanage.pojos.RepositoryQuestion;
import com.miukoo.aikao.qmanage.service.RepositoryQuestionService;
import com.miukoo.aikao.qmanage.service.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
* 语言设置 服务实现类
* </p>
*
* @author 聪明笨狗
* @since 2020-05-25 13:23
*/
@Service
public class RepositoryQuestionServiceImpl extends ServiceImpl<RepositoryQuestionMapper, RepositoryQuestion> implements RepositoryQuestionService {


    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public PageResDto<List<RepositoryQuestion>> paging(PageReqDto<RepositoryQuestionDto> reqDto) {

        //创建分页对象
        IPage<RepositoryQuestion> query = new Page<>(reqDto.getCurrent(), reqDto.getSize());

        //查询条件
        QueryWrapper<RepositoryQuestion> wrapper = new QueryWrapper<>();

        //获得数据
        IPage<RepositoryQuestion> page = this.page(query, wrapper);
        //转换结果
        return PageResDto.result(page.getTotal(),page.getRecords());
     }

    @Override
    public void saveAll(Long quId, Short quType, List<String> ids) {
        // 先删除
        QueryWrapper<RepositoryQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RepositoryQuestion::getQuId, quId);
        this.remove(wrapper);

        // 保存全部
        if(!CollectionUtils.isEmpty(ids)){
            List<RepositoryQuestion> list = new ArrayList<>();
            for(String id: ids){
                RepositoryQuestion ref = new RepositoryQuestion();
                ref.setQuId(quId);
                ref.setRepoId(Long.valueOf(id));
                ref.setQuType(quType);
                list.add(ref);
            }
            this.saveBatch(list);


            for(String id: ids){
                this.sortRepo(Long.valueOf(id));
            }
        }
    }


    @Override
    public List<String> listByQuestion(Long quId) {
        // 先删除
        QueryWrapper<RepositoryQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RepositoryQuestion::getQuId, quId);
        List<RepositoryQuestion> list = this.list(wrapper);
        List<String> ids = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for(RepositoryQuestion item: list){
                ids.add(item.getRepoId().toString());
            }
        }
        return ids;
    }

    @Override
    public List<Long> listByRepo(Long repoId, Short quType, boolean rand) {
        QueryWrapper<RepositoryQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(RepositoryQuestion::getRepoId, repoId);

        if(quType!=null){
            wrapper.lambda().eq(RepositoryQuestion::getQuType, quType);
        }

        if(rand){
            wrapper.orderByAsc(" RAND() ");
        }else{
            wrapper.lambda().orderByAsc(RepositoryQuestion::getSort);
        }

        List<RepositoryQuestion> list = this.list(wrapper);
        List<Long> ids = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for(RepositoryQuestion item: list){
                ids.add(item.getQuId());
            }
        }
        return ids;
    }

    @Override
    public void batchAction(RepositoryQuestionBatchReqDto reqDto) {

        // 移除的
        if(reqDto.getRemove()!=null &&  reqDto.getRemove()){
            QueryWrapper<RepositoryQuestion> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .in(RepositoryQuestion::getRepoId, reqDto.getRepoIds())
                    .in(RepositoryQuestion::getQuId, reqDto.getQuIds());
            this.remove(wrapper);
        }else{

            // 新增的
            for(String quId : reqDto.getQuIds()){
                Question q = questionMapper.selectById(quId);
                this.saveAll(Long.valueOf(quId), q.getType(), reqDto.getRepoIds());
            }
        }

        for(String id: reqDto.getRepoIds()){
            this.sortRepo(Long.valueOf(id));
        }

    }


    /**
     * 单个题库进行排序
     * @param repoId
     */
    private void sortRepo(Long repoId){

        QueryWrapper<RepositoryQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RepositoryQuestion::getRepoId, repoId);

        List<RepositoryQuestion> list = this.list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            return;
        }

        int sort = 1;
        for(RepositoryQuestion item: list){
            item.setSort(sort);
            sort++;
        }
        this.updateBatchById(list);

        // 更新统计数量
        repositoryService.refreshStat(repoId);
    }
}
